#!/bin/bash

cleanup()
{
  rm -rf /tmp/_bobcat_
  if [ $1 -ne 0 ]
  then
     rm -rf /opt/bobcat
  fi
  exit $1
}
if [ -f /opt/bobcat/bin/stopServer.sh ]
then
  /opt/bobcat/bin/stopServer.sh server1
  rm -rf /opt/bobcat
fi

if [ -d /tmp/_bobcat_ ]
then
    rm -rf /tmp/_bobcat_
fi
mkdir /tmp/_bobcat_
cd /tmp/_bobcat_
unzip $1 2>&1 >/dev/null
cd /
# Now install

/tmp/_bobcat_/install.sh -installroot /opt/bobcat -hostName localhost
if [ $? -ne 0 ]
then
   echo "Installation failed"
   cleanup 1
fi

sh /opt/bobcat/bin/expressPorts/UpdateExpressPorts.sh -portType http -server server1 -portName HTTPTransport_1 -vhostAlias HostAlias_1 -portValue 4411 2>&1 >/dev/null
sh /opt/bobcat/bin/expressPorts/UpdateExpressPorts.sh -portType http -server server1 -portName HTTPTransport_2 -vhostAlias HostAlias_2 -portValue 9099 2>&1 >/dev/null
sh /opt/bobcat/bin/expressPorts/UpdateExpressPorts.sh -portType http -server server1 -portName HTTPTransport_3 -vhostAlias HostAlias_3 -portValue 9098 2>&1 >/dev/null
sh /opt/bobcat/bin/expressPorts/UpdateExpressPorts.sh -removeAdminPorts

/opt/bobcat/bin/startServer.sh server1 -script /opt/bobcat/bin/start_server1.sh 2>&1 >/dev/null
if [ $? -ne 0 ]
then
   echo "Installation failed"
   cleanup 1
fi

if [ $? -ne 0 ]
then
   cleanup 1
fi
grep "^wasexpress" /etc/services 2>/dev/null
if [ $? -ne 0 ]
then
   echo "wasexpress       4411/tcp" >>/etc/services
   echo "wasexpress       4411/udp" >>/etc/services
fi

chmod 555 /opt/bobcat/bin/rc.was
grep "^was" /etc/inittab 2>/dev/null
if [ $? -ne 0 ]
then
   echo "was:2345:once:/opt/bobcat/bin/rc.was >/dev/console 2>&1" >>/etc/inittab
fi
cleanup 0
